
using task1_OrderSeriveXML;

namespace task1_OrderForm
{
    public partial class MainForm : Form
    {
        private OrderService OS { get; set; }
        private List<OrderGrid> ShowingOrderGrids { get; set; }
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            OS = new OrderService();
            Commodity book = new Commodity("book", 62);
            Commodity wood = new Commodity("wood", 25);
            Commodity tea = new Commodity("tea", 28);
            Commodity sugar = new Commodity("sugar", 100);
            Commodity mkf = new Commodity("phone", 211);

            /*Customers*/
            Customer Ava = new Customer("Ava", 40);
            Customer Bella = new Customer("Bella", 61);
            Customer Carol = new Customer("Carol", 19);

            /*OrderDetails*/
            OrderDetail od1 = new OrderDetail(book, 5, 0.9F);
            OrderDetail od2 = new OrderDetail(wood, 1);
            OrderDetail od3 = new OrderDetail(tea, 6);
            OrderDetail od4 = new OrderDetail(sugar, 1, 0.75F);
            OrderDetail od5 = new OrderDetail(mkf,4,2);
            Order order1 = new Order();
            Order order2 = new Order();
            Order order3 = new Order();
            order1.Id = OS.GetNextId();
            order1.Customer = Ava;
            order1.AddOrderDetail(od1);
            order1.AddOrderDetail(od2);
            order1.AddOrderDetail(od3);
            order2.Id = OS.GetNextId();
            order2.Customer = Bella;
            order2.AddOrderDetail(od4);
            order3.Id = OS.GetNextId();
            order3.Customer = Carol;
            order3.AddOrderDetail(od5);

            OS.AddOrder(order1);
            OS.AddOrder(order2);
            OS.AddOrder(order3);

            /*绑定数据*/
            ShowingOrderGrids = TransUtil.TransFromOrders(OS.SelectAll());
            orderBindingSource.DataSource = ShowingOrderGrids;
            detailBindingSource.DataSource = ShowingOrderGrids;
            detailBindingSource.DataMember = "OrderDetailGrids";

        }
        private void UpdateDataBinding(IEnumerable<Order> orders)        // 更新数据源
        {
            ShowingOrderGrids = TransUtil.TransFromOrders(orders);
            orderBindingSource.DataSource = ShowingOrderGrids;
            detailBindingSource.DataSource = ShowingOrderGrids;
        }

        

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int selectedRowIndex = orderDataGridView.CurrentCell.RowIndex;
            detailBindingSource.DataSource = ShowingOrderGrids[selectedRowIndex];
        }


        private void bindingSource2_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            OrderForm orderForm = new OrderForm(OS);
            orderForm.Text = "Add Order";
            orderForm.ShowDialog();
            if (orderForm.NewOrder != null)
            {
                OS.AddOrder(orderForm.NewOrder);
            }
            UpdateDataBinding(OS.SelectAll());
        }

        private void exportBtn_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.ShowDialog();
            string path = folderBrowserDialog.SelectedPath + "\\orders.xml";

            try
            {
                OS.Export(path);
            }
            catch (Exception)
            {
                MessageBox.Show("文件路径不存在！");
            }
        }

        private void importBtn_Click(object sender, EventArgs e)
        {
            openFileDialog.Filter = "XML文档|*.xml";
            openFileDialog.ShowDialog();
            string path = openFileDialog.FileName;

            try
            {
                OS.Import(path);
                UpdateDataBinding(OS.SelectAll());
            }
            catch (Exception)
            {
                MessageBox.Show("文件不存在或类型错误！");
            }
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            int selectedRowIndex = orderDataGridView.CurrentCell.RowIndex;
            OrderGrid selectedOrderGrid = ShowingOrderGrids[selectedRowIndex];
            OS.DeleteOrder(selectedOrderGrid.Id);
            UpdateDataBinding(OS.SelectAll());
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            SearchForm searchForm = new SearchForm(OS);
            searchForm.ShowDialog();        // 以阻塞的方式显示
            UpdateDataBinding(searchForm.SearchedOrders);
        }

        

        private void detailBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void updateBtn_Click(object sender, EventArgs e)
        {
 
                Console.WriteLine("111111");
                int selectedRowIndex = orderDataGridView.CurrentCell.RowIndex;
                OrderGrid selectedOrderGrid = ShowingOrderGrids[selectedRowIndex];
                int selectedId = selectedOrderGrid.Id;      // 获取要修改的对象
                Console.WriteLine("selectedId");
                Console.WriteLine("@@@@");
                OrderForm orderForm = new OrderForm(OS, OS.SearchWithId(selectedId));
                orderForm.Text = "Update Order";
                orderForm.ShowDialog();
                if (orderForm.NewOrder != null)
                {
                    OS.UpdateOrder(orderForm.NewOrder);
                }
                UpdateDataBinding(OS.SelectAll());
            
        }
    }
}